package com.example.dairenkun.neufoodshop.fragment;


import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.example.dairenkun.neufoodshop.R;


public class  CollectFragment extends Fragment implements View.OnClickListener {

    private LinearLayout mTabFood;
    private LinearLayout mTabShop;

    private TextView tvFood;
    private TextView tvShop;

    private Fragment mFmFood;
    private Fragment mFmShop;
     private  View view;

    public CollectFragment() {
        // Required empty public constructor

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

       View view=inflater.inflate(R.layout.fragment_collect, container, false);
        mTabShop=(LinearLayout)view.findViewById(R.id.id_tab_collectshop);
        mTabFood=(LinearLayout)view.findViewById(R.id.id_tab_collectfood);

        tvFood=(TextView)view.findViewById(R.id.collect_food_tv);
        tvShop=(TextView)view.findViewById(R.id.collect_shop_tv);
        //initViews();
        initEvents();
        setSelect(0);
       return  view;
    }
    private void initEvents(){
        mTabFood.setOnClickListener(this);
        mTabShop.setOnClickListener(this);
    }
private  void initViews(){
    mTabShop=(LinearLayout)view.findViewById(R.id.id_tab_collectshop);
    mTabFood=(LinearLayout)view.findViewById(R.id.id_tab_collectfood);

    tvFood=(TextView)view.findViewById(R.id.collect_food_tv);
    tvShop=(TextView)view.findViewById(R.id.collect_shop_tv);
}
   private void resetTextColors() {

       tvShop.setTextColor(ContextCompat.getColor(getActivity(), R.color.collect_color));
       tvFood.setTextColor(ContextCompat.getColor(getActivity(), R.color.collect_color));
    }

    @Override
    public void onClick(View v) {

        resetTextColors();
        switch (v.getId())
        {
            case R.id.id_tab_collectshop:
                setSelect(0);
                break;
            case R.id.id_tab_collectfood:
                setSelect(1);
                break;

            default:
                break;
        }
    }
    private void setSelect(int i) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mFmShop == null)
                {
                    mFmShop = new collect_shop_fragment();
                    transaction.add(R.id.id_collect_content, mFmShop);
                } else
                {
                    transaction.show(mFmShop);
                }
                tvShop.setTextColor(ContextCompat.getColor(getActivity(), R.color.collect_color_press));

                break;
            case 1:
                if (mFmFood == null)
                {
                    mFmFood = new collect_food_fragment();
                    transaction.add(R.id.id_collect_content, mFmFood);
                } else
                {
                    transaction.show(mFmFood);

                }

                tvFood.setTextColor(ContextCompat.getColor(getActivity(), R.color.collect_color_press));
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mFmShop != null)
        {
            transaction.hide(mFmShop);
        }
        if (mFmFood != null)
        {
            transaction.hide(mFmFood);
        }

    }
}
